/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.dspot.declex.actions;

import ohos.agp.components.Component;
import ohos.agp.components.element.Element;
import ohos.agp.components.element.FrameAnimationElement;
import ohos.app.Context;

import com.dspot.declex.annotation.action.ActionFor;

import org.ohosannotations.annotations.RootContext;

/**
 * 动画动作持有人
 *
 * @author Administrator
 * @since 2021-07-16
 */
@ActionFor("Animate")
public class AnimateActionHolder {

    /**
     * 上下文
     */
    @RootContext
    Context context;

    /**
     * 动画
     */
    private FrameAnimationElement animation;
    /**
     * 视图
     */
    private Component view;

    /**
     * 初始化
     *
     * @param view The View to which the animation will be applied
     * @param anim The resource id of the animation to load(AnimRes)
     */
    void init(Component view, int anim) {
        this.view = view;

        if (context != null) {
            this.animation = new FrameAnimationElement(context, anim);
//            this.animation = FrameAnimationElement.loadAnimation(context, anim);
        }
    }

    /**
     * 构建
     *
     * @param Ended <i><b>(default)</b></i> Notifies the end of the animation. This callback is not invoked
     * for animations with repeat count set to INFINITE.
     * @param Started Notifies the start of the animation.
     * @param Repeated Notifies the repetition of the animation.
     */
    void build(
        final Runnable Ended,
        final Runnable Started,
        final Runnable Repeated
    ) {

        if (animation == null) {
            return;
        }

//        this.animation.setAnimationListener(new Animation.AnimationListener() {
//            @Override
//            public void onAnimationStart(Animation animation) {
//                if (Started != null) Started.run();
//            }
//
//            @Override
//            public void onAnimationEnd(Animation animation) {
//                if (Ended != null) Ended.run();
//            }
//
//            @Override
//            public void onAnimationRepeat(Animation animation) {
//                if (Repeated != null) Repeated.run();
//            }
//        });
        this.animation.setCallback(new Element.OnChangeListener() {
            @Override
            public void onChange(Element element) {
                if (Started != null) {
                    Started.run();
                }
                if (Ended != null) {
                    Ended.run();
                }
                if (Repeated != null) {
                    Repeated.run();
                }
            }
        });
    }

    /**
     * 执行
     */
    void execute() {
        if (animation != null) {
//            view.startAnimation(animation);
            animation.start();
        }
    }

    /**
     * 动画
     *
     * @return Animation object that was loaded by the Action
     */
    public FrameAnimationElement animation() {
        return this.animation;
    }
}
